package org.example.tialsproject.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.tialsproject.mapper.EmpMapper;
import org.example.tialsproject.pojo.Emp;
import org.example.tialsproject.pojo.PageBean;
import org.example.tialsproject.pojo.User;
import org.example.tialsproject.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImp implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    /*获取员工列表的方法(支持条件查询、分页查询)*/
    @Override
    public PageBean fetchEmp(int page, int pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        /*
        Long count = empMapper.count();
        Integer start = (page - 1) * pageSize;
        List<Emp> empList = empMapper.fetchEmp(start, pageSize);
        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
        */
        PageHelper.startPage(page, pageSize);
        Page<Emp> p = (Page<Emp>) empMapper.fetchEmp(name, gender, begin, end);
        return new PageBean(p.getTotal(), p.getResult());
    }

    /*删除员工方法*/
    @Override
    public void deleteEmp(String ids) {
        empMapper.deleteEmp(ids);
    }

    /*添加员工的方法*/
    @Override
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        System.out.println(emp.getEntrydate());
        empMapper.addEmp(emp);
    }

    /*查询员工的方法*/
    @Override
    public Emp fetchEmpById(int id) {
        return empMapper.fetchEmpById(id);
    }

    /*编辑员工的方法*/
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    /*登录的方法*/
    @Override
    public Emp userLogin(User user) {
        return empMapper.userLogin(user);
    }
}
